<?php
/**
 * Anything added to this helper will be available to all templates in the News controller.
 */
 
/**
* 
*/
class NewsArticle 
{
    public $article;
    function __construct($article)
    {
        $this->story = $article;
    }
    
    function excerpt()
    {
        if ( strstr($this->article['content'],'{more}') !== false)
        {
            $econtent = explode('{more}',$this->article['content']);
            $link = link_to("Full Story &raquo;",
                            array('controller'=>'news' ,
                                  'action'=>'read/'.$this->article['index_name'].'#read_more'));
            $more = content_tag('div', $link, array('class'=>'article-read-more'));
                                       
            return $econtent[0].$more;                
        }
    }
    
    function content()
    {
        // return the content with the breaks and more stripped
        $content = str_replace('{more}','',$this->article['content']);
        $content = str_replace('{page_break}','', $content);
        return $content;
    }
    
    function content_paged($page_num)
    {
        if (strpos($this->article['content'],'{page_break}') === false) {
            return $this->content();
        }
        
        $article_sections = explode('{page_break}',$text);
		$this->total      = count($article_sections);
		$page_num         = (int) $page_num;
		$offset           = $page_num - 1; // offset to match array

		// do previous and next links:
		if ( $offset > 0 )
		{
			$this->previous = $offset;
		}
		
		// set next
		$this->next = $page_num + 1;
		if ( $this->next > $this->total )
		{
			$this->next = '';
		}
		
		// just show last page then.
		if ( $page_num > $this->total )
		{
			$offset = $this->total - 1;
			$this->next = '';
		}
		
		// make sure page_num isnt a negative number
		if ( $offset < 1 )
		{
			$offset = 0;
		}
		
		// set text = array num
		$text = $article_sections[$offset];
		// create as many links as count
		$_pages_nav = '';
		for ( $i=1; $i <= $this->total; $i++ )
		{ 
			$_pages_nav .= str_replace('{page_num}',$i,$pmatches[1]);
		}
	
		$paging_text = preg_replace("/".LD."num_pages".RD."(.*?)".LD.SLASH.'num_pages'.RD."/s",
									$_pages_nav,
									$paging_text);
		
			
			// find and replace tpl vars
			foreach ($TMPL->var_single as $key => $val)
			{
				switch ( $key )
				{
					case 'previous':
						// check previous == 1 or 0, so that we dont do dupe content:
						if ( $this->previous == 1 || $this->previous < 1 )
						{
							$paging_text = $TMPL->swap_var_single($val, '', $paging_text);
						} else {
							$paging_text = $TMPL->swap_var_single($val, $this->previous, $paging_text);
						}
					break;

					case 'next':
						$paging_text = $TMPL->swap_var_single($val, $this->next, $paging_text);
					break;

					case 'total':
						$paging_text = $TMPL->swap_var_single($val, $this->total, $paging_text);
					break;
				}
			}

			// create conditionals
			$cond['has_previous'] = ($this->previous>=1) ? 'TRUE' : 'FALSE';
			$cond['has_next'] = (is_numeric($this->next)) ? 'TRUE' : 'FALSE';
			$paging_text = $FNS->prep_conditionals($paging_text,$cond);
			
			// why loop through var_pair when you can just str_replace?
			$paging_text = str_replace(array(LD."page_navigation".RD,
											 LD.SLASH.'page_navigation'.RD,
											 LD."num_pages".RD,
											 LD.SLASH.'num_pages'.RD),
										"",
										$paging_text);
		
    }
    
    function file()
    {
        if ( !empty($this->article['file_id']) )
        {
            use_helper('admin/files');
            $path         = get_installed_path();
            $path_to_file = public_path_to_file($this->article['file_id']);
            // you can use w or h to change dimensions, plus all the other options for phpthumb
            $news_image   = "<img src='$path/img.php?src=$path/$path_to_file&h=100' class='news_image' />";
        }
    }
    
    function categories()
    {
        return $this->article['Categories'];
    }
    
    function date($format=null)
    {
        return format_date_string($this->article['publish_date'],$format);
    }
    
    function permalink()
    {
        return link_to($this->article['title'],
                       array('controller'=>'news',
                             'action'=>'read/'.$this->article['index_name']));
    }
}
?>